package com.ecarx.library_base.base_view;


import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ecarx.library_base.R;

public class BToast extends Toast {


    private static BToast toast;

    public BToast(Context context) {
        super(context);
    }

    public static void showToast(Context context, CharSequence text) {
        if (context == null || TextUtils.isEmpty(text)) {
            return;
        }
        //第二次点击及以后，它会先取消上一次的Toast, 然后show本次的Toast。
        if (toast != null) {
            toast.cancel();
        }
        toast = new BToast(context);
        View mView = LayoutInflater.from(context).inflate(R.layout.custom_toast, null);
        TextView mText = mView.findViewById(R.id.toast_show);
        mText.setText(text);
        toast.setView(mView);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void showToast(Context context, int resId) {
        showToast(context, context.getText(resId));
    }
}


